package com.zp.service;

import com.zp.api.ApiCode;
import com.zp.api.ApiResult;
import com.zp.container.MyRegisterContainer;
import com.zp.domain.RegisterDo;
import com.zp.dto.request.RegisterReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zp
 * @description
 * @created 2021/11/24 10:07
 */

@RequestMapping("/zp")
@RestController
public class RegisterService {
    @Autowired
    private MyRegisterContainer myRegisterContainer;

    /**
     * 注册服务
     */
    @PostMapping("/register")
    public ApiResult register(@RequestBody RegisterReq registerReq){
        return myRegisterContainer.put(registerReq)?ApiResult.ok():ApiResult.fail(ApiCode.FAIL);
    }

    /**
     * 活跃服务
     * @return
     */
    @GetMapping("/activeService")
    public ApiResult<Map<String, List<RegisterDo>>> activieService(){
        return ApiResult.ok(myRegisterContainer.activeService());
    }

    /**
     * 服务下线
     */
    @PostMapping("/remove")
    public ApiResult remove(@RequestBody RegisterReq registerReq){
        return myRegisterContainer.remove(registerReq)?ApiResult.ok():ApiResult.fail(ApiCode.FAIL);
    }

    /**
     *  服务续约
     * @param registerReq
     */
    @PostMapping("/renewal")
    public ApiResult renewal(@RequestBody RegisterReq registerReq){
        return myRegisterContainer.renewal(registerReq)?ApiResult.ok():ApiResult.fail(ApiCode.FAIL);
    }
}
